package com.sky.task;

import com.alibaba.fastjson.JSON;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 无商户号的定时测试类
 * 用于测试来单提醒
 */
@Component
@Slf4j
public class WebSocketTask {
    @Autowired
    private WebSocketServer webSocketServer;

    @Autowired
    private OrderMapper orderMapper;

    //解开注释即可测试
    //@Scheduled(cron = "0 * * * * ?") //每分钟执行一次
    public void updateOrderStatus(){
        String outTradeNo = "1724941770042";
        Orders ordersDB = orderMapper.getByNumber(outTradeNo);

        // 根据订单id更新订单的状态、支付方式、支付状态、结账时间
        Orders orders = Orders.builder()
                .id(ordersDB.getId())
                .status(Orders.TO_BE_CONFIRMED)
                .payStatus(Orders.PAID)
                .build();
        orderMapper.update(orders);

        // 通过websocket向客户端浏览器推送消息
        Map map = new HashMap<>();
        map.put("type",1); //1表示来单提醒 2表示客户催单
        map.put("orderId",ordersDB.getId()); //订单id
        map.put("content","订单号：" + outTradeNo);

        String json = JSON.toJSONString(map);

        webSocketServer.sendToAllClient(json);
    }
}
